interface IBody {
  code?: number;
  msg?: string;
  data?: any;
}
module.exports = {
  success(body: string | IBody) {
    let resolve = {
      code: 200,
      msg: 'success',
      data: null,
    };
    typeof body === 'string' ? resolve.msg = body : resolve = { ...resolve, ...body };
    this.body = resolve;
  },
  error(body: string | IBody) {
    let resolve = {
      code: 400,
      msg: '发生未知错误',
      data: null,
    };
    typeof body === 'string' ? resolve.msg = body : resolve = { ...resolve, ...body };
    this.body = resolve;
  },
  async getRedis(key: string) {
    const { app } = this;
    const res = await app.redis.get(key);
    return res;
  },
  async setRedis(key: string, value: any, expire?: number) {
    const { app } = this;
    let res = null;
    if (expire) {
      // 设置过期时间
      res = await app.redis.set(key, value, 'EX', expire);
    } else {
      // 不设置过期时间
      res = await app.redis.set(key, value);
    }
    return res;
  },
};